package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ClientHandleDistributeSocksClientProxyClientOtherConnectionTransferAdvanced extends
        AbstractHandleSocksDistributeClientProxyClientOtherConnectionTransferTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        String clientId = nettyProxyMsg.clientId();
        byte[] visitorId = nettyProxyMsg.getVisitorId();
        String targetIp = nettyProxyMsg.targetIp();
        Integer targetPort = Integer.parseInt(nettyProxyMsg.targetPort());

        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);

        NettySocksClientProxyClient nettySocksClientProxyClient = new NettySocksClientProxyClient();
        nettySocksClientProxyClient.setTargetClientId(clientId);
        nettySocksClientProxyClient.setNettyClientProperties(nettyClientProperties);
        nettySocksClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
        nettySocksClientProxyClient.setTargetIp(targetIp);
        nettySocksClientProxyClient.setTargetPort(targetPort);

        ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);

        // 创建真实通道
        NettySocksClientProxyClientRealSocket.buildRealServer(
                clientId,
                targetIp,
                targetPort,
                new String(visitorId),
                nettyClientProperties,
                handleChannelTypeAdvancedList
        );

    }
}
